//
//  AppDelegate.m
//  SmartWatch
//
//  Created by janshky on 2022/11/18.
//

#import "AppDelegate.h"
#import "ASTabBarViewController.h"
#import "HomeViewController.h"
#import "WatchViewController.h"
#import "ProfileViewController.h"
#import "LoginViewController.h"
#import "FaceParameterConfig.h"
#import "IDLFaceSDK/IDLFaceSDK.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [_notification addObserver:self selector:@selector(loginSuccessEvent) name:loginSuccess object:nil];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    
    if([ZWJCache isLogin]){
        self.window.rootViewController = [self setupTabBarController];
    }else{
        self.window.rootViewController = [[ASNavigationController alloc]initWithRootViewController:[LoginViewController new]];
    }
    
    // 初始化百度人脸识别
    [self initBaiDuFace];
    
    [self.window makeKeyAndVisible];
    
    return YES;
}

- (void)loginSuccessEvent {
    self.window.rootViewController = [self setupTabBarController];
}

- (ASTabBarViewController *)setupTabBarController{
    ASTabBarViewController *tabVC = [[ASTabBarViewController alloc] init];
    ASNavigationController *homeNav = [self createNavigationController:[HomeViewController new] tabbarTitle:@"首页" icon:@"tab_home_n" selectIcon:@"tab_home_s"];
    ASNavigationController *watchNav = [self createNavigationController:[WatchViewController new] tabbarTitle:@"设备" icon:@"tab_watch_n" selectIcon:@"tab_watch_s"];
    ASNavigationController *profileNav = [self createNavigationController:[ProfileViewController new] tabbarTitle:@"我的" icon:@"tab_profile_n" selectIcon:@"tab_profile_s"];
    tabVC.viewControllers = @[homeNav, watchNav, profileNav];
    tabVC.selectedIndex = 0;
    return  tabVC;
}

- (ASNavigationController *)createNavigationController:(UIViewController *)controller tabbarTitle:(NSString *)title icon:(NSString *)iconName selectIcon:(NSString *)selectIconName{
    ASNavigationController *nav = [[ASNavigationController alloc] initWithRootViewController:controller];
    controller.hidesBottomBarWhenPushed = NO;
    UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:title image:[UIImageMake(iconName) imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[UIImageMake(selectIconName) imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    controller.tabBarItem = item;
    return  nav;
}


- (void)initBaiDuFace {
    NSString* licensePath = [[NSBundle mainBundle] pathForResource:FACE_LICENSE_NAME ofType:FACE_LICENSE_SUFFIX];

    [[FaceSDKManager sharedInstance] setLicenseID:FACE_LICENSE_ID andLocalLicenceFile:licensePath andRemoteAuthorize:true];

    if (![[FaceSDKManager sharedInstance] canWork]){
       NSLog(@"授权失败，请检测ID 和 授权文件是否可用");
       return;
    }
}

@end
